package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.AppWarehouseCreateBo;
import org.dromara.system.domain.vo.AppWarehouseListVo;
import org.dromara.system.domain.vo.AppWarehouseSumVo;
import org.dromara.system.domain.vo.AppWarehouseTotalVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppWarehouseVo;
import org.dromara.system.domain.bo.AppWarehouseBo;
import org.dromara.system.service.IAppWarehouseService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 仓库
 *
 * @author Lion Li
 * @date 2025-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/warehouse")
public class AppWarehouseController extends BaseController {

    private final IAppWarehouseService appWarehouseService;

    /**
     * 仓库管理员 查看库存
     */
    @SaCheckPermission("system:warehouse:list")
    @GetMapping("/lookWareHouse")
    public R<List<AppWarehouseSumVo>> lookWareHouse(String query) {
        return appWarehouseService.lookWareHouse(query);
    }


    /**
     * 仓库管理员 库存管理
     */
    @SaCheckPermission("system:warehouse:list")
    @GetMapping("/wareHouseManage")
    public R<List<AppWarehouseTotalVo>> wareHouseManage(String query) {
        return appWarehouseService.queryByIdAndSearch(query);
    }


    /**
     * 根据公司id查询仓库列表
     */
    @SaCheckPermission("system:warehouse:list")
    @GetMapping("/listStoreByCompanyId")
    public R<List<AppWarehouseSumVo>> listByCompanyId(AppWarehouseBo bo) {
        return appWarehouseService.listStoreByCompanyId(bo);
    }


    /**
     * 查询仓库列表
     */
    @SaCheckPermission("system:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo<AppWarehouseVo> list(AppWarehouseBo bo, PageQuery pageQuery) {
        bo.setType(1);
        return appWarehouseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出仓库列表
     */
    @SaCheckPermission("system:warehouse:export")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppWarehouseBo bo, HttpServletResponse response) {
        List<AppWarehouseVo> list = appWarehouseService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库", AppWarehouseVo.class, response);
    }

    /**
     * 获取仓库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:warehouse:query")
    @GetMapping("/{id}")
    public R<AppWarehouseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appWarehouseService.queryById(id));
    }

    /**
     * 新增仓库
     */
    @SaCheckPermission("system:warehouse:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppWarehouseBo bo) {
        return toAjax(appWarehouseService.insertByBo(bo));
    }

    /**
     * 修改仓库
     */
    @SaCheckPermission("system:warehouse:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppWarehouseBo bo) {
        return toAjax(appWarehouseService.updateByBo(bo));
    }

    /**
     * 删除仓库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:warehouse:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appWarehouseService.deleteWithValidByIds(List.of(ids), true));
    }
}
